import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_payment_method.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_payment_method_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_payment_method_failure_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/yookassa/api_yookassa_bank_card_info_dto.dart';

const kApiWebPaymentMethodPaymentSystemValue = 'yookassa';

class ApiYookassaPaymentMethodDto extends Equatable implements ApiWebPaymentMethodDto {
  const ApiYookassaPaymentMethodDto._internal({
    required this.id,
    required this.userId,
    required this.saved,
    required this.test,
    required this.bankCardInfoDto,
    required this.failureDto,
  });

  @override
  final String id;

  @override
  final String userId;

  @override
  final bool saved;

  @override
  final bool test;

  @override
  final ApiYookassaBankCardInfoDto bankCardInfoDto;

  @override
  final ApiWebPaymentMethodFailureDto? failureDto;

  @override
  String get paymentSystem => kApiWebPaymentMethodPaymentSystemValue;

  @override
  List<Object?> get props {
    return [
      id,
      userId,
      saved,
      test,
      bankCardInfoDto,
      failureDto,
    ];
  }

  factory ApiYookassaPaymentMethodDto.fromEntity(ApiYookassaPaymentMethod paymentMethod) {
    final bankCardInfoDto = ApiYookassaBankCardInfoDto.fromEntity(paymentMethod.bankCardInfo);

    ApiWebPaymentMethodFailureDto? failureDto;

    if (paymentMethod.failure != null) {
      failureDto = ApiWebPaymentMethodFailureDto.fromEntity(paymentMethod.failure!);
    }

    return ApiYookassaPaymentMethodDto._internal(
      id: paymentMethod.id,
      userId: paymentMethod.userId,
      saved: paymentMethod.saved,
      test: paymentMethod.test,
      bankCardInfoDto: bankCardInfoDto,
      failureDto: failureDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiWebPaymentMethodIdKey: id,
      kApiWebPaymentMethodUserIdKey: userId,
      kApiWebPaymentMethodSavedKey: saved,
      kApiWebPaymentMethodTestKey: test,
      kApiWebPaymentMethodPaymentSystemKey: paymentSystem,
      kApiWebPaymentMethodFailureKey: failureDto?.toString(),
      kApiWebPaymentMethodBankCardInfoKey: bankCardInfoDto.toJson(),
    };
  }
}
